<template>
  <div>
    <!--批量删除按钮-->
    <el-button size="mini" type="danger" @click="handleDeleteAll">
      <i class="el-icon-delete"></i>批量删除
    </el-button>
    <!--添加按钮-->
    <el-button size="mini" type="primary" @click="handleAdd">
      <i class="el-icon-plus"></i>添加
    </el-button>
    <!--表格区域-->
    <el-table
        :data="tableData"
        @selection-change="handleSelectionChange"
        style="width: 100%">
      <!--多选框-->
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <!--每个列,prop是后端返回数据的属性-->
      <el-table-column
          prop="roleId"
          label="角色ID"
          width="100">
      </el-table-column>
      <el-table-column
          prop="roleName"
          label="角色"
          width="100">
      </el-table-column>
      <!--        // 密码列-->
      <el-table-column
          prop="permName"
          label="数据权限"
          width="150">
      </el-table-column>
<!--      &lt;!&ndash;操作列&ndash;&gt;-->
<!--      <el-table-column label="操作">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--              size="mini"-->
<!--              @click="handleEdit(scope.$index, scope.row)">编辑-->
<!--          </el-button>-->
<!--          <el-button-->
<!--              size="mini"-->
<!--              type="danger"-->
<!--              @click="handleDelete(scope.$index, scope.row)">删除-->
<!--          </el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>
    <!--分页区域-->
    <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!--添加角色弹窗-->
    <el-dialog
        :title=title
        :visible.sync="dialogFormVisible"
        width="30%">
      <!--表单-->
      <el-form  ref="addRoleForm" :model="addRoleForm" label-width="80px">
        <!--角色名标签-->
        <el-form-item label="角色名" prop="roleName">
          <el-input v-model="addRoleForm.roleName" placeholder="请输入角色名"></el-input>
        </el-form-item>
        <!--数据权限标签-->
        <el-form-item label="数据权限" prop="permName">
          <el-input v-model="addRoleForm.permName" placeholder="请输入数据权限"></el-input>
        </el-form-item>
      </el-form>
      <div v-if="type==='add'" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('addUserForm')">确定</el-button>
        <el-button @click="dialogFormVisible = false">取消</el-button>
      </div>
<!--      <div v-if="type==='update'"  slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="updateForm('addUserForm')">确定</el-button>-->
<!--        <el-button @click="dialogFormVisible = false">取消</el-button>-->
<!--      </div>-->
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "RoleView",
  data() {
    return {
      tableData:[],
      idarr: [],
      pageNum: 1,
      pageSize: 3,
      total: null,
      addRoleForm: {
        roleName: '',
        permName: ''
      },
      //添加表单的显示状态--添加表单
      dialogFormVisible: false,
      //添加表单的显示状态--更新表单
      dialogFormVisibleUpdate: false,
    }
  },
  methods: {
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.addRoleForm.roleName = ''
      this.addRoleForm.permName = ''
      this.pageNum = 1
      this.pageSize = 3
      this.fetchData()
    },
    //获取所有角色
    fetch: function () {
      axios.get('/user/selectPermission',
      ).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.data.length
      })
    },
    //获取选中的行数据
    handleSelectionChange(val) {
      console.log(val)
      this.idarr = []
      val.forEach(item => {
        this.idarr.push(item.id)
      })
      console.log(this.idarr)
    },
    handleDeleteAll() {
    },
    //添加按钮
    handleAdd() {
      this.dialogFormVisible = true
      this.title = '添加角色'
      this.type = 'add'
      this.addRoleForm = {
        roleName: '',
        permName: ''
      }
      this.resetForm('addRoleForm')
    },
    // 分页大小改变时触发
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.fetchData()
    },
    // 分页当前页改变时触发
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.fetchData()
    },
    //表单添加按钮
    submitForm() {
      this.title = '添加角色'
          axios.post('/role/add', this.addRoleForm)
              .then(response => {
                console.log(response)
                if (response.code === 2000) {
                  this.$message.success('添加成功');
                  //页数重置为1
                  this.pageNum = 1
                  this.fetchData()
                  //关闭弹窗
                  this.dialogFormVisible = false
                }
              },
              error => {
                this.$message.error('添加失败');
              })
        }
    },
    // //表单修改
    // updateForm() {
    //   axios.post('/user/update', this.addUserForm)
    //       .then(response => {
    //         console.log(response)
    //         if (response.code === 2000) {
    //           this.$message.success('修改成功');
    //           //页数重置为1
    //           this.pageNum = 1
    //           this.fetchData()
    //           //关闭弹窗
    //           this.dialogFormVisible = false
    //         }
    //       })
    //
    //           }

  created() {
    this.fetch()
  }
}
</script>

<style scoped>

</style>